<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="doc">

  <xsl:output method="xml" media-type="application/pdf"/>
  
  <xsl:param name="_apply-mode"/>
  
  <xsl:param name="ref"/>
  
  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="$_apply-mode = 'doc-info'">
        <author>HTTP Unit</author>
        <title>PDF Test</title>
        <keywords>LivCos PDF Test load</keywords>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="document($ref)/*"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="/*">
    <root>
      <layout-master-set>
        <simple-page-master master-name="page_A4" page-height="29.7cm" page-width="21cm"
          margin-top="1.5cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm">
          <region-body margin-top="1.6cm" margin-bottom="1cm" margin-left="0.5cm"
            margin-right="0.5cm" />
          <region-before extent="1.6cm" />
          <region-after extent="1cm" />
        </simple-page-master>
      </layout-master-set>
      <page-sequence master-reference="page_A4">
        <static-content flow-name="xsl-region-before">
          <table table-layout="fixed" width="100%" border-bottom-style="solid" border-bottom-width="thin">
            <table-column column-width="20%"/>
            <table-column column-width="60%"/>
            <table-column column-width="20%"/>
            <table-body>
              <table-row>
                <table-cell padding="0pt">
                  <block text-align="start">PDF Test</block>
                </table-cell>
                <table-cell padding="0pt">
                  <block font-size="10pt" font-family="sans-serif" text-align="center"
                    margin-top="15pt">
                    <xsl:value-of select="$ref" />
                  </block>
                </table-cell>
                <table-cell padding="0pt">
                  <block font-size="10pt" font-family="sans-serif" text-align="end" margin-top="15pt">
                    <page-number />/<page-number-citation ref-id="last_page" />
                  </block>
                </table-cell>
              </table-row>
            </table-body>
          </table>
        </static-content>
        <static-content flow-name="xsl-region-after">
          <table table-layout="fixed" width="100%" border-top-style="solid" border-top-width="thin">
            <table-column column-width="20%"/>
            <table-column column-width="60%"/>
            <table-column column-width="20%"/>
            <table-body>
              <table-row>
                <table-cell padding="0pt">
                  <block text-align="start">PDF Test</block>
                </table-cell>
                <table-cell padding="0pt">
                  <block font-size="10pt" font-family="sans-serif" text-align="center"
                    margin-top="15pt">
                    <xsl:value-of select="$ref" />
                  </block>
                </table-cell>
                <table-cell padding="0pt">
                  <block font-size="10pt" font-family="sans-serif" text-align="end" margin-top="15pt">
                    <page-number />/<page-number-citation ref-id="last_page" />
                  </block>
                </table-cell>
              </table-row>
            </table-body>
          </table>
        </static-content>
        <flow flow-name="xsl-region-body" font-size="10pt" font-family="Verdana">
          <block margin-left="0.4cm" font-size="8pt" margin-top="0pt" margin-bottom="0pt">
            <xsl:apply-templates select="node()" mode="markup"/>
          </block>
          <block id="last_page"></block>
        </flow>
      </page-sequence>
    </root>
  </xsl:template>

  <!-- basic elements -->
  <xsl:template match="*" mode="markup">
    <block margin-left="0.9cm" color="blue" text-indent="-0.6cm">
      <xsl:text><![CDATA[<]]></xsl:text>
      <xsl:value-of select="name(.)"/>
      <xsl:apply-templates select="@*" mode="markup"/>
      <xsl:call-template name="namespace_markup"/>
      <xsl:text><![CDATA[>]]></xsl:text>
    </block>
    <block margin-left="0.3cm">
      <xsl:apply-templates select="node()" mode="markup"/>
    </block>
    <block margin-left="0.3cm" color="blue">
      <xsl:text><![CDATA[<]]>/</xsl:text>
      <xsl:value-of select="name(.)"/>
      <xsl:text><![CDATA[>]]></xsl:text>
    </block>
  </xsl:template>
  
  <!-- elements with direct text children -->
  <xsl:template match="*[normalize-space(string-join(text(), '')) != '']" mode="markup">
    <block margin-left="0.9cm" color="blue" text-indent="-0.6cm">
      <xsl:text><![CDATA[<]]></xsl:text>
      <xsl:value-of select="name(.)"/>
      <xsl:apply-templates select="@*" mode="markup"/>
      <xsl:call-template name="namespace_markup"/>
      <xsl:text><![CDATA[>]]></xsl:text>
      <xsl:apply-templates select="node()" mode="markup"/>
      <xsl:text><![CDATA[<]]>/</xsl:text>
      <xsl:value-of select="name(.)"/>
      <xsl:text><![CDATA[>]]></xsl:text>
    </block>
  </xsl:template>
  
  <!-- empty elements -->
  <xsl:template match="*[count(node()) &lt; 1]" mode="markup">
    <xsl:choose>
      <xsl:when test="normalize-space(../text()) != ''">
        <inline color="blue">
          <xsl:call-template name="print_emptyElement"/>
        </inline>
      </xsl:when>
      <xsl:otherwise>
        <block margin-left="0.9cm" color="blue" text-indent="-0.6cm">
          <xsl:call-template name="print_emptyElement"/>
        </block>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="print_emptyElement">
    <xsl:text><![CDATA[<]]></xsl:text>
    <xsl:value-of select="name(.)"/>
    <xsl:apply-templates select="@*" mode="markup"/>
    <xsl:call-template name="namespace_markup"/>
    <xsl:text>/<![CDATA[>]]></xsl:text>
  </xsl:template>
  
  <!-- attributes -->
  <xsl:template match="@*" mode="markup">
    <xsl:text> </xsl:text>
    <inline color="green">
      <xsl:value-of select="name(.)"/>
    </inline>
    <xsl:text>=</xsl:text>
    <inline color="black">
      <xsl:text>"</xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>"</xsl:text>
    </inline>
  </xsl:template>
  
  <!-- namespaces -->
  <xsl:template name="namespace_markup">
    <xsl:variable name="e" select="." />
    <xsl:for-each select="namespace::*[name(.) != 'xml']">
      <xsl:variable name="n" select="name(.)" />
      <xsl:if test="not($e/ancestor::*[namespace::*[name(.) = $n]])">
        <xsl:text> </xsl:text>
        <inline color="green">
          <xsl:text>xmlns:</xsl:text>
          <xsl:value-of select="name(.)"/>
        </inline>
        <xsl:text>=</xsl:text>
        <inline color="black">
          <xsl:text>"</xsl:text>
          <xsl:value-of select="."/>
          <xsl:text>"</xsl:text>
        </inline>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  
  <!-- comments -->
  <xsl:template match="comment()" mode="markup">
    <block margin-left="0.3cm" color="grey">
      <xsl:text><![CDATA[<]]>!--</xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>--<![CDATA[>]]></xsl:text>
    </block>
  </xsl:template>
  
  <!-- text -->
  <xsl:template match="text()" mode="markup">
    <inline color="black">
      <xsl:value-of select="normalize-space(.)"/>
    </inline>
  </xsl:template>
  
  <!-- processing instructions -->
  <xsl:template match="processing-instruction()" mode="markup">
    <block margin-left="0.3cm" color="darkgrey">
      <xsl:text><![CDATA[<]]>?xml</xsl:text>
      <xsl:value-of select="."/>
      <xsl:text>?<![CDATA[>]]></xsl:text>
    </block>
  </xsl:template>
  
  
  
</xsl:stylesheet>
